import { Toast } from "vant";
import { getFriendAndGroupApply , getDynamicCount } from "@/request/footer-request";

export const useGetFriendAndGroupApply = async (app,store, _id) => {
  try {
    const notifys = await getFriendAndGroupApply(_id);
    if (notifys.code !== 201 || notifys.message !== "success") {
      Toast(notifys.message);
    } else {
      let friends = [];
      let groups = [];
      if (notifys.data.length > 0) {
        friends = notifys.data.filter((cur) => cur.type === "private");
        groups = notifys.data.filter((cur) => cur.type === "group");

        store.dispatch("notify/setNotifyMessage", {
          type: "friend",
          count: friends.length,
          data: friends,
        });

        store.dispatch("notify/setGroupNotifyMessage", {
          type: "group",
          count: groups.length,
          data: groups,
        });

        app.$bus.emit("setNotifys",{
          friend:{
            list:friends,
            type:"friend"
          },
          group:{
            list:groups,
            type:"group"
          },
        })

        return notifys.data.length;
      }
    }
  } catch (error) {
    Toast(error.message);
  }
};

export const useGetNewDynamicCount = async (store,_id) => {
  try{
      const result = await getDynamicCount(_id);
      if(result.code === 201 && result.message === "success"){
        const num = result.data[0] ? result.data[0].new_dynamic_nums : '';
        store.dispatch("dynamic/setNewDynamicCount",num || 0)
        return num;
      }else{
        return 0;
      }
  }catch(error){
     Toast(error.message)
  }
}